package com.easybuy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;

import com.alibaba.fastjson.JSON;
import com.easybuy.entily.Product_category;
import com.easybuy.entily.User;
import com.easybuy.service.Product_categoryService;
import com.easybuy.service.UserService;
import com.easybuy.serviceimpl.Product_categoryServiceImpl;
import com.easybuy.serviceimpl.UserServiceImpl;

public class UserServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public UserServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		
		UserService us=new UserServiceImpl();
		String opr=request.getParameter("opr");
		if("add".equals(opr)){//ע��
			User u=new User();
			u.setLoginName(request.getParameter("loginName"));
			u.setUserName(request.getParameter("userName"));
			u.setPassword(DigestUtils.md5Hex(request.getParameter("password")));
			u.setSex(Integer.valueOf(request.getParameter("sex")));
			u.setIdentityCode(request.getParameter("identityCode"));
			u.setEmail(request.getParameter("email"));
			u.setMobile(request.getParameter("mobile"));
			//u.setType(Integer.valueOf(request.getParameter("type")));
			int res=us.addUser(u);
			if(res==1){
				out.print("true");
			}else{
				out.print("false");   
			}

		}else if("all".equals(opr)){
			List<User> list=us.SelectUser();
			String json=JSON.toJSONString(list);
			out.print(json);
		}else if("del".equals(opr)){
			int id=Integer.valueOf(request.getParameter("id"));
			int res=us.delNewsInfo(id);
			if(res>=1){
				out.print("true");
			}else{
				out.print("false");
			} 
		}else if("login2".equals(opr)){
			
			String loginName=request.getParameter("loginName");
			String password=request.getParameter("password");
			User user=us.UserByLoginName(loginName,DigestUtils.md5Hex(password));
			if(user!=null){
				out.print("true");
				request.getSession().setAttribute("user2",user.getUserName());
				request.getSession().setAttribute("type2",user.getType());
				request.getSession().setAttribute("sex2",user.getSex());
				request.getSession().setAttribute("email2",user.getEmail());
				request.getSession().setAttribute("mobile2",user.getMobile());
				request.getSession().setAttribute("loginname2",user.getLoginName());
			}else{
				out.print("false");
			}
		}else if("zhuxiao".equals(opr)){
			request.getSession().invalidate();
			response.sendRedirect("pre/Login.jsp");
		}else if("updata".equals(opr)){
			User u=new User();
			u.setLoginName(request.getParameter("loginName"));
			u.setUserName(request.getParameter("userName"));
			u.setPassword(request.getParameter("password"));
			u.setEmail(request.getParameter("email"));
			u.setId(Integer.valueOf(request.getParameter("id")));
			u.setIdentityCode(request.getParameter("identityCode"));
			u.setMobile(request.getParameter("mobile"));
			u.setSex(Integer.valueOf(request.getParameter("sex")));
			u.setType(Integer.valueOf(request.getParameter("type")));
			int res=us.updateNewsInfo(u);
			if(res==1){
				out.print("true");
			}else{
				out.print("false");
			}
		}
		
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		doGet(request,response);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
